package org.gp953.pet.service.impl;

import org.gp953.basic.service.impl.BaseServiceImpl;
import org.gp953.basic.util.*;
import org.gp953.order.domain.PetAcquisitionOrder;
import org.gp953.order.mapper.PetAcquisitionOrderMapper;
import org.gp953.org.domain.Employee;
import org.gp953.org.domain.Shop;
import org.gp953.org.mapper.EmployeeMapper;
import org.gp953.org.mapper.ShopMapper;
import org.gp953.pet.domain.Pet;
import org.gp953.pet.domain.SearchMasterMsg;
import org.gp953.pet.mapper.SearchMasterMsgMapper;
import org.gp953.pet.query.SearchMasterMsgQuery;
import org.gp953.pet.service.IPetService;
import org.gp953.pet.service.ISearchMasterMsgService;
import org.gp953.user.domain.LoginInfo;
import org.gp953.user.domain.User;
import org.gp953.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        // 获取当前登录用户
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        User user = userMapper.queryByLoginInfoId(loginInfo.getId());
        searchMasterMsg.setUser_id(user.getId());
        searchMasterMsg.setUser(user);

        // 获取最近的店铺
        //先把地址转化为经纬度的点
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        List<Shop> shops = shopMapper.loadAll();
        // 在通过工具类获取最近的店铺
        Shop nearestShop = DistanceUtil.getNearestShop(point,shops);
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsg.setShop_id(nearestShop.getId());
        // 设置状态为待处理State=0
        searchMasterMsg.setState(0);
        // 保存寻主消息
        searchMasterMsgMapper.save(searchMasterMsg);

    }


    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long id) {
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        // 待处理的为0
        query.setState(0);
        return super.queryPageData(query);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long id) {
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        // 已经处理的为1
        query.setState(1);
        return super.queryPageData(query);
    }

    @Override
    public PageList<SearchMasterMsg> queryUserMsg(SearchMasterMsgQuery query, Long id) {
        User user = userMapper.queryByLoginInfoId(id);
        query.setUserId(user.getId());
        return super.queryPageData(query);
    }

    /**
     * 处理宠物信息
     * @param pet
     * @param request
     */
    @Override
    public void handle(Pet pet, HttpServletRequest request) {
        // 获取当前用户id
        Long id = LoginContext.getLoginInfo(request).getId();
        //修该订单状态
        searchMasterMsgMapper.updateMsgState(pet.getSearch_master_msg_id());
        // 保存宠物详情
        petService.add(pet);
        System.out.println(pet);
        // 通过当前后台登录人的id去查询出该员工的id，用于记录收购员工是谁
        Long empId = employeeMapper.loadByLoginInfoId(id).getId();
        // 通过宠物中的Search_master_msg_id获取收购宠物的寻主信息
        SearchMasterMsg adopt = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        // 通过宠物信息，该宠物的寻主信息，和该收购员工的信息生成收购订单
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, adopt, empId);
        petAcquisitionOrderMapper.save(petAcquisitionOrder);
        // @TODO 支付
        System.out.println("宠物" + pet.getName()+"的收购价格是："+pet.getCostprice());

    }

    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt,Long empId) {
        // 生成空订单
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        // 设置订单摘要信息
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        // 设置订单的状态
        order.setState(0);//待支付
        // 设置订单收购成本价格
        order.setPrice(pet.getCostprice());
        // 设置订单的收购地址，在什么地方收购的宠物
        order.setAddress(adopt.getAddress());
        // 通过工具类CodeGenerateUtils的generateOrderSn()方法生成订单的唯一编号
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        // 设置订单的唯一编号
        order.setOrderSn(orderSn);
        // 设置订单中的宠物id
        order.setPet_id(pet.getId());
        // 设置订单中的前任主人的id信息
        order.setUser_id(adopt.getUser_id());
        // 设置订单中的支付状态，即平台通过什么方式付款给宠物出售人
        order.setPaytype(0);
        // 设置订单中收购店铺的id
        order.setShop_id(pet.getShop_id());
        // // 设置订单中的收购店铺的收购员的id信息
        order.setEmp_id(empId);
        return order;
    }
}
